<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <read@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use manniu\controller\AdminBase;

use app\admin\model\AdminAuthRule as AdminAuthRuleModel;

class AuthRule extends AdminBase{
    //当前控制器菜单
 	public $submenu = [];

 	/**
     * TP初始化方法
     */
    public function _initialize() {
        parent::_initialize();
        $this->submenu[] = ['title' => '权限规则列表', 'url' => url('admin/AuthRule/index'), 'icon' => '&#xe60a;'];
        $this->submenu[] = ['title' => '权限规则添加', 'url' => url('admin/AuthRule/add'), 'icon' => '&#xe654;'];
    }

    /**
     * 权限规则管理
     * @access public
     * @return \think\Response
     */
    public function index() {
        $list = AdminAuthRuleModel::order('id desc')->paginate(config('mn_admin.admin_pagesize'));
    	$this->assign([
            'list' => $list,
            'submenu' => $this->submenu,
        ]);
    	return $this->fetch();
    }

    /**
     * 添加
     * @access public
     * @return \think\Response
     */
    public function add() {
        $auth_rule_model = new AdminAuthRuleModel;
        if($this->request->isPost()){
            //获取提交的数据
            $data = $this->request->param('data/a');
            // 验证规则名称是否重复
            if($auth_rule_model->where('title',$data['title'])->count()){
                $this->error('规则名称已存在');
            }
            // 验证规则标识是否重复
            if($auth_rule_model->where('name',$data['name'])->count()){
                $this->error('规则标识已存在');
            }
            if($auth_rule_model->allowField(true)->validate(true)->save($data)){
                $auth_rule_model->cache();
                $this->success('权限规则添加成功','index');
            }else{
                $this->error($auth_rule_model->getError());
            }
        }
        $this->assign([
            'submenu' => $this->submenu,
        ]);
        return $this->fetch();
    }

    /**
     * 修改
     * @access public
     * @return \think\Response
     */
    public function edit($id = 0) {
        $id = (int)$id;
        if(!$id){
            $this->error('缺少规则参数ID');
        }
        $data = AdminAuthRuleModel::get($id);
        if(!$data){
            $this->error('规则不存在');
        }
        // 表单提交操作
        if($this->request->isPost()){
            $post_data = $this->request->param('data/a');
            // 系统默认规则不允许修改
            if($id == 1 || $id == 2 || $id == 3 || $id == 4){
                $this->error('系统默认规则不允许修改','index');
            }
            // 验证规则名称是否重复
            if($data->where('id','<>',(int)$data->id)->where('title',$post_data['title'])->count()){
                $this->error('规则名称已存在');
            }
            // 验证规则标识是否重复
            if($data->where('id','<>',(int)$data->id)->where('name',$post_data['name'])->count()){
                $this->error('规则标识已存在');
            }
            if(false !== $data->allowField(true)->validate(true)->save($post_data,['id'=>(int)$data->id])){
                $this->success('规则修改成功','index');
            }else{
                $this->error($data->getError());
            }
        }
        // 模版变量赋值
        $this->assign([
            'data' => $data,
            'submenu' => $this->submenu,
        ]);
        return $this->fetch();
    }

    /**
     * 删除
     * @access public
     * @return void
     */
    public function del($id = 0) {
        $id = (int)$id;
        if(!$id){
            $this->error('缺少规则参数ID');
        }
        if($id == 1 || $id == 2 || $id == 3 || $id == 4){
            $this->error('系统默认规则不允许删除','index');
        }
        $data = AdminAuthRuleModel::get($id);
        if(!$data){
            $this->error('规则不存在');
        }
        $data->delete();
        $this->success('规则删除成功', 'index');
    }
}